package cn.promonkey.core.base;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import cn.promonkey.core.dto.UserDto;
import cn.promonkey.core.toolkit.UserInfoUtils;

import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;

import java.util.Date;

/** @author 郭晓宇 */
@Component
public class BaseMetaObjectHandler implements MetaObjectHandler {


  @Override
  public void insertFill(MetaObject metaObject) {
    // 获取当前登录用户ID
    UserDto login = UserInfoUtils.getUserInfo();
    if (null != login) {
      //this.setFieldValByName("businessId", login.getBusinessId(), metaObject);
      this.setFieldValByName("createId", login.getUserId(), metaObject);
      this.setFieldValByName("updateId", login.getUserId(), metaObject);
    }else {
    	this.setFieldValByName("createId", 0l, metaObject);
        this.setFieldValByName("updateId", 0l, metaObject);
    }
    this.setFieldValByName("createTime", new Date(), metaObject);
    this.setFieldValByName("updateTime", new Date(), metaObject);
    this.setFieldValByName("delFlag", 1, metaObject);
  }

  @Override
  public void updateFill(MetaObject metaObject) {
    // 获取当前登录用户ID
    UserDto login = UserInfoUtils.getUserInfo();
    if (null != login) {
      this.setFieldValByName("updateId", login.getUserId(), metaObject);
    }else {
      this.setFieldValByName("updateId", 0l, metaObject);
    }
    this.setFieldValByName("updateTime", new Date(), metaObject);
  }
}
